using System.Collections.ObjectModel;
using HMCollada.FX;
using System.Xml;

namespace HMCollada.FX {
    internal struct HMColladaInstanceMaterial {
        internal string sid;
        internal string name;
        internal string target;
        internal string symbol;
        internal Collection<HMColladaBind> bind;
        internal Collection<HMColladaBindVertexInput> bind_vertex_input;
    }

    internal struct HMColladaBindVertexInput {
        internal string semantic;
        internal string input_semantic;
        internal int input_set;
    }
}

namespace HMCollada {
    internal partial class HMColladaLoader {
        private static HMColladaInstanceMaterial LoadInstanceMaterial() {
            HMColladaInstanceMaterial newInstanceMaterial = new HMColladaInstanceMaterial();
            newInstanceMaterial.sid = colladaReader.GetAttribute("sid");
            newInstanceMaterial.name = colladaReader.GetAttribute("name");
            newInstanceMaterial.target = colladaReader.GetAttribute("target");
            newInstanceMaterial.symbol = colladaReader.GetAttribute("symbol");

            newInstanceMaterial.bind = new Collection<HMColladaBind>();
            newInstanceMaterial.bind_vertex_input = new Collection<HMColladaBindVertexInput>();

            if (!colladaReader.IsEmptyElement) colladaReader.Read();
            do {
                if (colladaReader.NodeType != XmlNodeType.EndElement) {
                    switch (colladaReader.Name) {
                        case "bind": newInstanceMaterial.bind.Add(LoadBind()); break;
                        case "bind_vertex_input": newInstanceMaterial.bind_vertex_input.Add(LoadBindVertexInput()); break;
                    }
                }

                if (!colladaReader.IsEmptyElement) colladaReader.Read();
            } while (colladaReader.Name != "instance_material");

            if (colladaReader.IsEmptyElement) colladaReader.Read();
            return newInstanceMaterial;
        }

        private static HMColladaBindVertexInput LoadBindVertexInput() {
            HMColladaBindVertexInput newBindVertexInput = new HMColladaBindVertexInput();
            newBindVertexInput.semantic = colladaReader.GetAttribute("semantic");
            newBindVertexInput.input_semantic = colladaReader.GetAttribute("input_semantic");
            newBindVertexInput.input_set = int.Parse(colladaReader.GetAttribute("input_set"));

            if (colladaReader.IsEmptyElement) colladaReader.Read();
            return newBindVertexInput;
        }
    }
}